GBML manual / Maths

roll_chance

True roughly `percent` times in every hundred it is asked - 0 never, 100 always. For "does this happen this frame?": a flicker, a spark, a rare drop.

Syntax

roll_chance(percent) -> bool

Arguments

NameType
percentreal

Returns

bool

Description

Answers "does this happen?" - true roughly percent times out of every hundred it is asked. 0 never, 100 always.

This is the shape most randomness in a 2D game actually wants: a torch that skips a frame now and then, a spark off a ricochet, a chest that sometimes holds the good thing. Written out of a range roll it is easy to get wrong by one; written as a percentage it says what it means.

The argument is a percentage, not a fraction between 0 and 1 - roll_chance(25) is a quarter of the time, so the number in the code is the number you were already thinking in.

Example

if (roll_chance(4)) {
    instance_create(x, y, obj_doc_spark);
}

A torch that throws a spark now and then. At 60 frames a second, 4 percent per step is roughly two or three sparks a second - a rate you tune by moving one number, with no range arithmetic in between to get wrong.

Both ends are exact, and cost nothing

0 (or anything below it) never happens; 100 (or anything above it) always does. Both exactly - not "almost never" and "almost always", which is the usual rounding artifact in a percentage check built by hand.

Neither of those two cases costs a draw from the stream. That is worth knowing if you gate something on a variable that is sometimes sitting at 0: a disabled effect does not consume rolls and shift everything else in the game along with it.

In between, the odds are exact to about a four-thousandth of a percent, with no rounding step to skew them.

Rolls repeat, on purpose

Two runs of the same ROM answer the same way in the same order, unless something calls roll_reseed. See roll_int for why that is the default, and for what a save file does and does not carry.

On the GBA

Both ends are exact, not almost: 0 or below never happens, 100 or above always does, and neither costs a draw - so gating something on a variable that happens to be sitting at 0 does not quietly consume the stream and shift every later roll. In between, the odds are exact to a four-thousandth of a percent, with no rounding step to skew them.

See also: roll_introll_realroll_reseedroll_one_of